package com.it.mydream.Controller;

import com.it.mydream.Bean.Lei;
import com.it.mydream.Bean.User;
import com.it.mydream.Service.imp.ShaoleiServiceImp;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class ShaoleiController {

    @Autowired
    private ShaoleiServiceImp slService;

    public void createShaoleiTable(HttpServletRequest request){
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        User user = (User) session.getAttribute("user");
        slService.setUser(user);
        Lei[][] leilist_t = (Lei[][]) application.getAttribute("leilist");
        if(leilist_t!=null){

        }else{
            Lei[][] leilist = slService.creatShaoleiTable(20,10,40);
            application.setAttribute("leilist", leilist);
        }

    }

    @RequestMapping("getShaoleiTable")
    @ResponseBody
    public Map<String,Object> getShaoleiTable(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        Lei[][] leilist = (Lei[][]) application.getAttribute("leilist");
        Map<String, Object> map = new HashMap<>();
        map.put("leilist",leilist);
        return map;
    }

    @RequestMapping("clickShaolei")
    @ResponseBody
    public void clickShaoleiTable(HttpServletRequest request){
        int x = Integer.parseInt(request.getParameter("x"));
        int y = Integer.parseInt(request.getParameter("y"));
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        slService.setUser(user);
        slService.clickShaoleiTable(x,y);
    }

    @RequestMapping("reset")
    @ResponseBody
    public void reset(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        Lei[][] leilist = slService.creatShaoleiTable(20,10,40);
        application.setAttribute("leilist", leilist);
    }

}
